//
//  GameScene.cpp
//  CandyDizzle
//
//  Created by Cloner Team on 8/2/14.
//
//

#include "GameScene.h"
float IncreaseSpeed::multiply = 1.0f;

GameScene::GameScene(){
    this->createLayers();
    this->createLayersContent();
    this->scheduleUpdate();
    this->initialize();
}

GameScene::~GameScene(){
    
}

void GameScene::onEnter() {
    AbstractScene::onEnter();
}

void GameScene::onExit() {
    AbstractScene::onExit();
    this->unscheduleUpdate();
    this->removeAllChildrenWithCleanup(true);
}
void GameScene::initialize() {
    GameModel* gameModel = GameModel::getInstance();
    this->gameListener = GameListener::create();
    this->gameListener->setHandlerObj(this);
    this->gameListener->setNewColor(ug_listener_func(GameScene::onFireNewColor));
    this->gameListener->setIncreaseSpeed(ug_listener_func(GameScene::onFireIncreaseSpeed));
    this->gameListener->setReturnToNormal(ug_listener_func(GameScene::onFireReturnToNormal));
    gameModel->addListener(this->gameListener);
}

void GameScene::createLayers() {
    GameModel* gameModel = GameModel::getInstance();
    //background layer
    this->backgroundLayer = UGLayer::create();
    this->backgroundLayer->setPosition(Point::ZERO);
    this->addChild(backgroundLayer);
    
    //guilayer
    this->guiLayer = GUILayer::create();
    this->guiLayer->setPosition(Point::ZERO);
    this->addChild(this->guiLayer);
    
    //gameLayer
    this->gameLayer = UGLayer::create();
    this->gameLayer->setPosition(Point::ZERO);
    this->addChild(this->gameLayer);
}

void GameScene::createLayersContent() {
    GameModel* gameModel = GameModel::getInstance();
    Size gameSize = gameModel->getDisplayResolutionSize();
    
    /*----------BACKGROUND LAYER----------*/
    //background
    Face* background = new Face();
    background->initWithFile((char*)IMG_BACKGROUND);
    background->setPosition(Point(gameSize.width/2,gameSize.height/2));
    this->backgroundLayer->addChild(background);
}

void GameScene::update(float dt){
    GameModel* gameModel = GameModel::getInstance();
    Size gameSize = gameModel->getDisplayResolutionSize();
    gameModel->update(dt);
    this->guiLayer->update(dt);
    //finishGame
    if(gameModel->getGameState() == GS_PLAY) {
        Color* color = gameModel->getColorQueue().front();
        if (color->label->getPositionY() <= 487){
            this->notify(REQ_FINISH_GAME, EVT_DEFAULT, NULL, 0, NULL);
        }
    }
}

void GameScene::onFireNewColor(Ref* data){
    GameModel* gameModel = GameModel::getInstance();
    Size gameSize = gameModel->getDisplayResolutionSize();
    Color* newColor = gameModel->getColorQueue().back();
    Label* newColorLabel = newColor->label;
    newColorLabel->setPosition(gameSize.width/2,gameSize.height + 300);
    this->gameLayer->addChild(newColorLabel);
    
    ActionInterval* moveToAction = MoveTo::create(5.4f, Point(gameSize.width/2, 487));
    IncreaseSpeed* increaseSpeedAction = IncreaseSpeed::create(moveToAction, 1.0f);
    increaseSpeedAction->setTag(0);
    newColorLabel->runAction(increaseSpeedAction);
}

void GameScene::onFireIncreaseSpeed(Ref* data) {
    IncreaseSpeed::multiply += 0.7f;
}

void GameScene::onFireReturnToNormal(Ref* data) {
    IncreaseSpeed::multiply = 1.0f;
}

void IncreaseSpeed::step(float dt) {
    _innerAction->step(dt * _speed * IncreaseSpeed::multiply);
}

IncreaseSpeed* IncreaseSpeed::create(ActionInterval *action, float speed) {
    IncreaseSpeed *ret = new IncreaseSpeed();
    if (ret && ret->initWithAction(action, speed))
    {
        ret->autorelease();
        return ret;
    }
    CC_SAFE_DELETE(ret);
    return nullptr;
}
